package com.person.jc.utils;

import java.math.BigDecimal;

/**
 * ***************************************************************************
 * @程序名: NumHelper.java
 * @日期 : Sep 7, 2015
 * @作者 : 武智雄   (wuzhixiong@vvise.com)
 * @模块 : [项目]-[一级菜单]-[二级菜单]-[数字处理类] 
 * @描述 : [功能简介] 
 * ------------------------------------------------------------ 
 * 修改历史 
 * 序号             日期                      修改人                  修改原因 
 * 1
 * 2
*****************************************************************************
 */
public class NumHelper {

    /**
     * 
     * @Description: [把Str转化为double]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param obj
     * @@return
     */
    public static double getDobleByStr(Object obj){
        double val = 0;
        try{
            val = Double.valueOf(String.valueOf(obj));
        }catch (Exception e) {
            e.printStackTrace();
            val = 0;
        }
        
        return val;
    }
    
    /**
     * 
     * @Description: [把String转化为BigDecimal]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param str
     * @@return
     */
    public static BigDecimal getBigDecimalByStr(Object str){
        BigDecimal val = new BigDecimal(0);
        try{
            val = new BigDecimal(String.valueOf(str));
        }catch (Exception e) {
            e.printStackTrace();
        }
        
        return val;
    }
    
    /**
     * 
     * @Description: [将为0字符串转化为空，主要针对小数文本]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param obj
     * @@return
     */
    public static String convertZEROToEmpty(Object obj){
        String str = String.valueOf(obj);
        if(isZERO(obj))
            str = "";
        
        return str;
    }
    
    /**
     * 
     * @Description: [判断是否为0]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param obj
     * @@return
     */
    public static boolean isZERO(Object obj){
        boolean is0 = false;
        BigDecimal val = getBigDecimalByStr(obj);
        if(val.compareTo(BigDecimal.ZERO) == 0)
            is0 = true;
        
        return is0;
    }
    
    /**
     * 
     * @Description: [判断单个字符是否为数字]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param c
     * @@return
     */
    public static boolean isNumber(char c){
        
        return Character.isDigit(c);
    }
    
    /**
     * 
     * @Description: [将金额小写转化为大写]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param obj
     * @@return
     */
    public static String moneyToChs(Object obj){
        try{
            double n = getBigDecimalByStr(obj).doubleValue();
            String fraction[] = {"角", "分"};
            String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            String unit[][] = {{"元", "万", "亿"},
                         {"", "拾", "佰", "仟"}};
     
            String head = n < 0? "负": "";
            n = Math.abs(n);
             
            String s = "";
            for (int i = 0; i < fraction.length; i++) {
                double f1= new BigDecimal(n).setScale(2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(10 * Math.pow(10, i))).doubleValue(); 
                s += (digit[(int) (Math.floor(f1) % 10)] + fraction[i]).replaceAll("(零.)+", ""); 
    //            s += (digit[(int)(Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
            }
            if(s.length()<1){
                s = "整";    
            }
            int integerPart = (int)Math.floor(n);
     
            for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
                String p ="";
                for (int j = 0; j < unit[1].length && n > 0; j++) {
                    p = digit[integerPart%10]+unit[1][j] + p;
                    integerPart = integerPart/10;
                }
                s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
            }
            return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
        }catch(Exception e){
            e.printStackTrace();
            return "零";
        }
    }
    
    /**
     * 
     * @Description: [如果小数位后全部为0的话，将小数位去掉]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param str
     * @@return
     */
    public static String delPrecisionZERO(Object str){
        BigDecimal num = getBigDecimalByStr(str);
        
        return delPrecisionZERO(num);
    }
    
    /**
     * 
     * @Description: [如果小数位后全部为0的话，将小数位去掉]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: Sep 7, 2015
     * @@param num
     * @@return
     */
    public static String delPrecisionZERO(BigDecimal num){
        String result = String.valueOf(num.toString());
        BigDecimal intNum = getBigDecimalByStr(num.intValue());
        if(num.compareTo(intNum) == 0){
            result = intNum.toString();
        }
        
        return result;
    }
}